﻿Imports System.Data
Imports System.Data.SqlClient
Imports Abyx.WindowsExtendedControls

Namespace DB

    ''' <summary>
    ''' TypeName:    CustomerType
    ''' Author:      GeneratedCustomTemplate
    ''' DBTableName: SVC_Customers
    ''' </summary>
    ''' <remarks></remarks>

    Public Class CustomerType
        Inherits DBTableObjectBase

#Region "Declarations"
        'Protected Friend iCustomerId as Guid
        Protected Friend iCustomerNo As String
        Protected Friend iIdType As String
        Protected Friend iIdNo As String
        Protected Friend iName As String
        Protected Friend iAddress As String
        Protected Friend iTel As String
        Protected Friend iMob As String
#End Region

#Region "Constructors"
        Public Sub New()
            MyBase.New(TableNamesEnum.SVC_Customers)
        End Sub
        Public Sub New(ByVal CustomerId As Guid)
            MyBase.New(TableNamesEnum.SVC_Customers, CustomerId)
        End Sub
        Public Sub New(ByVal CustomerId As Guid, _
                        ByVal CustomerNo As String, _
                        ByVal IdType As String, _
                        ByVal IdNo As String, _
                        ByVal Name As String, _
                        ByVal Address As String, _
                        ByVal Tel As String, _
                        ByVal Mob As String, _
                        ByVal LMUserId As Guid, _
                        ByVal LMDate As DateTime)
            MyBase.New(TableNamesEnum.SVC_Customers)
            Me.iId = CustomerId
            Me.iCustomerNo = CustomerNo
            Me.iIdType = IdType
            Me.iIdNo = IdNo
            Me.iName = Name
            Me.iAddress = Address
            Me.iTel = Tel
            Me.iMob = Mob
            Me.iLMUserId = LMUserId
            Me.iLMDate = LMDate

        End Sub
#End Region

#Region "Exposed Properties"
        Public Property CustomerId() As Guid
            Get
                Return Me.iId
            End Get
            Set(ByVal value As Guid)
                Me.iId = value
            End Set
        End Property
        Public Property CustomerNo() As String
            Get
                Return Me.iCustomerNo
            End Get
            Set(ByVal value As String)
                Me.iCustomerNo = value
            End Set
        End Property
        Public Property IdType() As String
            Get
                Return Me.iIdType
            End Get
            Set(ByVal value As String)
                Me.iIdType = value
            End Set
        End Property
        Public Property IdNo() As String
            Get
                Return Me.iIdNo
            End Get
            Set(ByVal value As String)
                Me.iIdNo = value
            End Set
        End Property
        Public Property Name() As String
            Get
                Return Me.iName
            End Get
            Set(ByVal value As String)
                Me.iName = value
            End Set
        End Property
        Public Property Address() As String
            Get
                Return Me.iAddress
            End Get
            Set(ByVal value As String)
                Me.iAddress = value
            End Set
        End Property
        Public Property Tel() As String
            Get
                Return Me.iTel
            End Get
            Set(ByVal value As String)
                Me.iTel = value
            End Set
        End Property
        Public Property Mob() As String
            Get
                Return Me.iMob
            End Get
            Set(ByVal value As String)
                Me.iMob = value
            End Set
        End Property
#End Region

#Region "Implementations"
        Protected Friend Overloads Function Load(ByVal Row As DataRow) As Boolean
            If IsDBNull(Row(0)) Then Me.iId = Guid.Empty Else Me.iId = Row(0)
            If IsDBNull(Row(1)) Then Me.iCustomerNo = Nothing Else Me.iCustomerNo = Row(1)
            If IsDBNull(Row(2)) Then Me.iIdType = Nothing Else Me.iIdType = Row(2)
            If IsDBNull(Row(3)) Then Me.iIdNo = Nothing Else Me.iIdNo = Row(3)
            If IsDBNull(Row(4)) Then Me.iName = Nothing Else Me.iName = Row(4)
            If IsDBNull(Row(5)) Then Me.iAddress = Nothing Else Me.iAddress = Row(5)
            If IsDBNull(Row(6)) Then Me.iTel = Nothing Else Me.iTel = Row(6)
            If IsDBNull(Row(7)) Then Me.iMob = Nothing Else Me.iMob = Row(7)
            If IsDBNull(Row(8)) Then Me.iLMUserId = Nothing Else Me.iLMUserId = Row(8)
            If IsDBNull(Row(9)) Then Me.iLMDate = Nothing Else Me.iLMDate = Row(9)

            Return True
        End Function
        Protected Friend Overrides Function Load() As Boolean
            'sp_[TableName]Get
            Dim resBool As Boolean = True
            Dim da As New SqlDataAdapter
            Dim cmd As New SqlCommand("sp_" & Me.iTableName.ToString.Replace("COMP_", "") & "Get '" & Me.iId.ToString("D") & "'")
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction

            Dim dt As DataTable = New DataTable
            dt = Utility.GetList(cmd)

            If dt.Rows.Count > 0 Then
                Me.Load(dt.Rows(0))
            Else
                resBool = False
            End If

            Return resBool
        End Function
        Protected Friend Overrides Function Insert() As Boolean
            'sp_[TableName]Insert
            Dim resBool As Boolean = True
            Dim resInt As Integer
            Dim da As New SqlDataAdapter

            Dim cmd As New SqlCommand("sp_" & Me.iTableName.ToString.Replace("COMP_", "") & "Insert")
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction
            'Set command type to Stoed Procedure
            cmd.CommandType = CommandType.StoredProcedure
            'Validation or required parameters
            Try
                'Populate Stored Procedure parameters
                With cmd.Parameters
                    If IsNothing(Me.iId) Then
                        .AddWithValue("@CustomerId", System.DBNull.Value)
                    Else
                        .AddWithValue("@CustomerId", Me.iId)
                    End If
                    If IsNothing(Me.iCustomerNo) Then
                        .AddWithValue("@CustomerNo", System.DBNull.Value)
                    Else
                        .AddWithValue("@CustomerNo", Me.iCustomerNo)
                    End If
                    If IsNothing(Me.iIdType) Then
                        .AddWithValue("@IdType", System.DBNull.Value)
                    Else
                        .AddWithValue("@IdType", Me.iIdType)
                    End If
                    If IsNothing(Me.iIdNo) Then
                        .AddWithValue("@IdNo", System.DBNull.Value)
                    Else
                        .AddWithValue("@IdNo", Me.iIdNo)
                    End If
                    If IsNothing(Me.iName) Then
                        .AddWithValue("@Name", System.DBNull.Value)
                    Else
                        .AddWithValue("@Name", Me.iName)
                    End If
                    If IsNothing(Me.iAddress) Then
                        .AddWithValue("@Address", System.DBNull.Value)
                    Else
                        .AddWithValue("@Address", Me.iAddress)
                    End If
                    If IsNothing(Me.iTel) Then
                        .AddWithValue("@Tel", System.DBNull.Value)
                    Else
                        .AddWithValue("@Tel", Me.iTel)
                    End If
                    If IsNothing(Me.iMob) Then
                        .AddWithValue("@Mob", System.DBNull.Value)
                    Else
                        .AddWithValue("@Mob", Me.iMob)
                    End If
                    If IsNothing(Me.iLMUserId) Then
                        .AddWithValue("@LMUserId", System.DBNull.Value)
                    Else
                        .AddWithValue("@LMUserId", Me.iLMUserId)
                    End If
                    If IsNothing(Me.iLMDate) Then
                        .AddWithValue("@LMDate", System.DBNull.Value)
                    Else
                        .AddWithValue("@LMDate", Me.iLMDate)
                    End If

                End With
                'Execute Stored Procedure
                resInt = cmd.ExecuteNonQuery()
            Catch ex As Exception
                resBool = False
                Throw ex
            End Try
            If resInt = 0 Then
                Throw New RequestedRecordDoesNotExistException("Requested changes failed to insert, no records were modified")
            End If
            Return resBool
        End Function
        Protected Friend Overrides Function Update() As Boolean
            'sp_[TableName]Update
            Dim resBool As Boolean = True
            Dim resInt As Integer
            Dim da As New SqlDataAdapter
            Dim cmd As New SqlCommand("sp_" & Me.iTableName.ToString.Replace("COMP_", "") & "Update")
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction
            'Set command type to Stoed Procedure
            cmd.CommandType = CommandType.StoredProcedure
            'Validation or required parameters
            Try
                'Populate Stored Procedure parameters
                With cmd.Parameters
                    If IsNothing(Me.iId) Then
                        .AddWithValue("@CustomerId", System.DBNull.Value)
                    Else
                        .AddWithValue("@CustomerId", Me.iId)
                    End If
                    If IsNothing(Me.iCustomerNo) Then
                        .AddWithValue("@CustomerNo", System.DBNull.Value)
                    Else
                        .AddWithValue("@CustomerNo", Me.iCustomerNo)
                    End If
                    If IsNothing(Me.iIdType) Then
                        .AddWithValue("@IdType", System.DBNull.Value)
                    Else
                        .AddWithValue("@IdType", Me.iIdType)
                    End If
                    If IsNothing(Me.iIdNo) Then
                        .AddWithValue("@IdNo", System.DBNull.Value)
                    Else
                        .AddWithValue("@IdNo", Me.iIdNo)
                    End If
                    If IsNothing(Me.iName) Then
                        .AddWithValue("@Name", System.DBNull.Value)
                    Else
                        .AddWithValue("@Name", Me.iName)
                    End If
                    If IsNothing(Me.iAddress) Then
                        .AddWithValue("@Address", System.DBNull.Value)
                    Else
                        .AddWithValue("@Address", Me.iAddress)
                    End If
                    If IsNothing(Me.iTel) Then
                        .AddWithValue("@Tel", System.DBNull.Value)
                    Else
                        .AddWithValue("@Tel", Me.iTel)
                    End If
                    If IsNothing(Me.iMob) Then
                        .AddWithValue("@Mob", System.DBNull.Value)
                    Else
                        .AddWithValue("@Mob", Me.iMob)
                    End If
                    If IsNothing(Me.iLMUserId) Then
                        .AddWithValue("@LMUserId", System.DBNull.Value)
                    Else
                        .AddWithValue("@LMUserId", Me.iLMUserId)
                    End If
                    If IsNothing(Me.iLMDate) Then
                        .AddWithValue("@LMDate", System.DBNull.Value)
                    Else
                        .AddWithValue("@LMDate", Me.iLMDate)
                    End If

                End With
                'Execute Stored Procedure
                resInt = cmd.ExecuteNonQuery()
            Catch ex As Exception
                resBool = False
                Throw ex
            End Try
            If resInt = 0 Then
                Throw New RequestedRecordDoesNotExistException("Requested changes failed to update, no records were modified")
            End If
            Return resBool
        End Function
        Protected Friend Overrides Function ValidateOnDelete() As Boolean
            Return True
        End Function
        Protected Friend Overrides Function ValidateOnInsert() As Boolean
            Return True
        End Function
        Protected Friend Overrides Function ValidateOnUpdate() As Boolean
            Return True
        End Function
#End Region

#Region "Overrides"
        Public Overrides Function ToString() As String
            If Me.iIdNo <> "" Then
                Return Me.iIdNo & " - " & Me.iName
            Else
                Return Me.iName
            End If
        End Function
        Public Overrides Function ToDetailsListBoxItem() As WindowsExtendedControls.DetailListBoxItem

            Dim header As String = ""
            Dim details As String = ""
            Dim footer As String = ""
            If Me.iCustomerNo <> "" Then
                header = Me.iCustomerNo & " - " & Me.iName
            Else
                If Me.iIdNo <> "" Then
                    header = Me.iIdNo & " - " & Me.iName
                Else
                    header = Me.iName
                End If
            End If
            If Me.iCustomerNo <> "" AndAlso Me.iIdNo <> "" Then
                details = Me.iIdNo
            End If
            If Me.iAddress <> "" Then
                details = details & vbNewLine & Me.iAddress
            End If
            If Me.iMob <> "" Then
                footer = Me.iMob
            Else
                If Me.iTel <> "" Then
                    footer = Me.iTel
                End If
            End If
            Return New DetailListBoxItem(header, details, footer, Nothing, Me)

        End Function
#End Region

    End Class

    Public Class CustomerTypeCollection
        Inherits DBTableObjectCollectionBase

        Public DataFilters As New DataFilterTypeCollection(Of CustomerTypeFields)(Me.iTablename.ToString)
        Public DataSorters As New DataSorterTypeCollection(Of CustomerTypeFields)(Me.iTableName.ToString)

        Public Sub New()
            MyBase.New(TableNamesEnum.SVC_Customers)
        End Sub

        Default Public Property Item(ByVal Index As Integer) As CustomerType
            Get
                Return CType(Me.List.Item(Index), CustomerType)
            End Get
            Set(ByVal value As CustomerType)
                Me.List.Item(Index) = value
            End Set
        End Property

        Protected Friend Overrides Function Load() As Boolean
            Return Me.Load("SELECT * FROM " & Me.iTableName.ToString & Me.DataFilters.GetSQL & Me.DataSorters.GetSQL)
        End Function

        Protected Friend Overloads Function Load(ByVal CustomFilter As String) As Boolean
            'Get data from DB
            Dim cmd As New SqlCommand
            cmd.Connection = Me.iConnection
            If IsNothing(Me.iTransaction) = False Then cmd.Transaction = Me.iTransaction
            Dim dt As DataTable
            Try
                cmd.CommandText = CustomFilter
                dt = Utility.GetList(cmd)
            Catch ex As Exception
                Throw ex
            End Try
            'Clear exising list
            Dim row As DataRow
            List.Clear()

            'Build new list
            Dim typeObject As CustomerType

            For Each row In dt.Rows
                typeObject = New CustomerType()
                typeObject.iConnection = Me.iConnection
                typeObject.iTransaction = Me.iTransaction
                If typeObject.Load(row) = True Then Me.List.Add(typeObject)
            Next
            Return True
        End Function

        Public Overloads Sub Sort(ByVal propertyToSort As CustomerTypeFields, ByVal sortOrder As SortOrder)
            ipropertyToSort = propertyToSort.ToString
            isortOrder = sortOrder
            Me.InnerList.Sort(Me)
        End Sub

    End Class

#Region "Enumerations"       'Enumerations
    Public Enum CustomerTypeFields
        CustomerId
        CustomerNo
        IdType
        IdNo
        Name
        Address
        Tel
        Mob
        LMUserId
        LMDate

    End Enum
#End Region

End Namespace